package com.chuantou.yuba.util;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;

import com.chuantou.yuba.api.to.UserTO;
import com.chuantou.yuba.ui.ChatActivity;
import com.chuantou.yuba.ui.LoginActivity;
import com.chuantou.yuba.ui.OthersProfileActivity;
import com.chuantou.yuba.ui.ProfileDetailActivity;
import com.chuantou.yuba.ui.ReportActivity;
import com.easemob.chat.EMMessage;
import com.easemob.chat.EMMessage.Direct;
import com.easemob.exceptions.EaseMobException;

public class ActivityHelper {

	public static void toProfileActivity(Activity activity, long uid) {
		Context context = activity.getApplicationContext();
		AccountManager manager = AccountManager.getInstance(context);
		if (manager.isLogined()) {
			UserTO user = manager.getUserTO();
			if (user.getUid() == uid) {
				Intent intent = new Intent(activity, ProfileDetailActivity.class);
				activity.startActivity(intent);
			} else {
				Intent intent = new Intent(activity, OthersProfileActivity.class);
				intent.putExtra(Constants.EXTRA_UID, uid);
				activity.startActivity(intent);
			}
		} else {
			UiUtil.showNoLoginDialog(activity, -1, LoginActivity.ACTION_RETURN);
		}
	}

	public static void toChatActivity(Activity activity, long uid, String nickname, String avatar) {
		Intent intent = new Intent(activity, ChatActivity.class);
		intent.putExtra(ChatActivity.EXTRA_UID, ChatUtils.getHXAccount(uid));
		intent.putExtra(ChatActivity.EXTRA_NICKNAME, nickname);
		intent.putExtra(ChatActivity.EXTRA_AVATAR, avatar);
		intent.putExtra(ChatActivity.EXTRA_CHAT_TYPE, ChatActivity.CHATTYPE_SINGLE);
		activity.startActivity(intent);
	}

	public static void toChatActivity(Activity activity, String hxAccount, String nickname, String avatar) {
		Intent intent = new Intent(activity, ChatActivity.class);
		intent.putExtra(ChatActivity.EXTRA_UID, hxAccount);
		intent.putExtra(ChatActivity.EXTRA_NICKNAME, nickname);
		intent.putExtra(ChatActivity.EXTRA_AVATAR, avatar);
		intent.putExtra(ChatActivity.EXTRA_CHAT_TYPE, ChatActivity.CHATTYPE_SINGLE);
		activity.startActivity(intent);
	}

	public static void toChatActivity(Activity activity, EMMessage message) {
		Intent intent = getToChatIntent(activity, message);
		if (intent != null) {
			activity.startActivity(intent);
		}
	}

	public static Intent getToChatIntent(Context context, EMMessage message) {
		try {
			Intent intent = new Intent(context, ChatActivity.class);
			String uid = message.direct == Direct.SEND ? message.getTo() : message.getFrom();
			intent.putExtra(ChatActivity.EXTRA_UID, uid);
			intent.putExtra(ChatActivity.EXTRA_NICKNAME, ChatUtils.getChatNickname(message));
			intent.putExtra(ChatActivity.EXTRA_AVATAR, ChatUtils.getChatAvatar(message));
			intent.putExtra(ChatActivity.EXTRA_CHAT_TYPE, ChatActivity.CHATTYPE_SINGLE);
			return intent;
		} catch (EaseMobException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public static void toReportActivity(Activity activity,long uid){
		Intent intent = new Intent(activity, ReportActivity.class);
		intent.putExtra(Constants.EXTRA_UID, uid);
		activity.startActivity(intent);
	}
}
